//
//  ZYFilterView.m
//  YiLin
//
//  Created by Zyark on 2018/6/7.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYFilterSettingView.h"
#import "ZYUtils.h"
#import "ZYModeTypeModel.h"
#import "ZYImageUtils.h"
#import "ZYCustomSlider.h"
#import "ZYViewUtils.h"

@interface ZYFilterSettingView()

/**
 模式图片
 */
@property(nonatomic,strong) UIImageView *modeTypeImageView;

/**
 模式名称
 */
@property(nonatomic,strong) UILabel *modeNameLabel;

/**
 当前小时数
 */
@property(nonatomic,strong) UILabel *hourLabel;

/**
 系统默认小时数
 */
@property(nonatomic,strong) UILabel *systemLabel;


/**
 系统温默认小时数指示图片
 */
@property(nonatomic,strong) UIImageView *systemImageView;


/**
 小时数进度 颜色渐变
 */
@property(nonatomic,strong) ZYCustomSlider *hourSlider;


/**
 已经使用
 */
@property(nonatomic,strong) UILabel *alreadyUsedLabel;


/**
 清零
 */
@property(nonatomic,strong) UIButton *clearBtn;

@end

@implementation ZYFilterSettingView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
    _maxValue = 3500;
    _minValue = 0;
    CGFloat imageWidth = self.width * 0.13;
    //模式图片
    _modeTypeImageView = [[UIImageView alloc] init];
    _modeTypeImageView.size = CGSizeMake(imageWidth, imageWidth);
    [self addSubview:_modeTypeImageView];
    _modeTypeImageView.left = 17;
    _modeTypeImageView.top = imageWidth * 0.3;
    
    //模式名称
    _modeNameLabel = [[UILabel alloc] init];
    _modeNameLabel.textColor = kMainColor;
    _modeNameLabel.font = [UIFont systemFontOfSize:14];
    _modeNameLabel.size = CGSizeMake(imageWidth * 3, imageWidth  * 0.4);
    [self addSubview:_modeNameLabel];
    _modeNameLabel.top = _modeTypeImageView.top;
    _modeNameLabel.left = _modeTypeImageView.right + 20;
    
    //进度 颜色渐变
    CGFloat progressWidth = self.width - _modeTypeImageView.right - 30;
    _hourSlider = [[ZYCustomSlider alloc] initWithFrame:CGRectMake(0, 0, progressWidth, imageWidth * 0.4) withTrackHeight: imageWidth * 0.1];
    [self addSubview:_hourSlider];
    _hourSlider.left = _modeTypeImageView.right + 15;
    _hourSlider.top = _modeTypeImageView.bottom - 10;
    UIColor *startColor = [UIColor colorWithHexString:kMainColorEndColorHexString];
    UIColor *endColor = [UIColor colorWithHexString:kMainColorStartColorHexString];
    NSArray *colors = @[startColor,endColor];
    UIImage *img = [ZYUtils createImageWithSize:_hourSlider.size gradientColors:colors percentage:@[@0.4,@1] gradientType:ZYGradientFromLeftToRight];
    [_hourSlider setMinimumTrackImage:img forState:UIControlStateNormal];
    [_hourSlider setMaximumTrackTintColor:RGBA(235, 235, 235, 1)];
    [_hourSlider setMinimumValue: 10.0f];
    [_hourSlider setMaximumValue:_maxValue];
    CGRect rect = CGRectMake(0, 0, imageWidth * 0.4, imageWidth * 0.4);
    UIImage *image = [ZYImageUtils imageWithCornerRadius:rect.size.width / 2 image:[ZYImageUtils imageWithColor:kMainColor rect:rect]];
    [_hourSlider setThumbImage:image forState:UIControlStateNormal];
    [_hourSlider setContinuous:NO];//滑动结束通知
    [_hourSlider addTarget:self
                           action:@selector(valueChange:)
                 forControlEvents:UIControlEventValueChanged];
    
    //当前值
    _hourLabel = [[UILabel alloc] init];
    _hourLabel.size = CGSizeMake(100, imageWidth * 0.4);
    _hourLabel.textColor = RGBA(153, 153, 153, 1);
    _hourLabel.font = [UIFont systemFontOfSize:14];
    _hourLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_hourLabel];
    _hourLabel.right = _hourSlider.right;
    _hourLabel.centerY= _modeNameLabel.centerY;
    
    //系统标示
    _systemImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_quick_eqtup_system"]];
    [self addSubview:_systemImageView];
    _systemImageView.top = _hourSlider.bottom - 10;
    _systemImageView.centerX = _hourSlider.left + ((_systemValue-_minValue)/(_maxValue - _minValue)) * _hourSlider.width;
    
    _systemLabel = [[UILabel alloc] init];
    _systemLabel.size = CGSizeMake(100, imageWidth * 0.4);
    _systemLabel.textColor = RGBA(153, 153, 153, 1);
    _systemLabel.font = [UIFont systemFontOfSize:12];
    _systemLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_systemLabel];
    _systemLabel.centerX = _systemImageView.centerX;
    _systemLabel.top = _systemImageView.bottom + 5;
    
    _alreadyUsedLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _hourSlider.width / 2, 15)];
    _alreadyUsedLabel.font = [UIFont systemFontOfSize:14];
    _alreadyUsedLabel.textAlignment = NSTextAlignmentLeft;
    _alreadyUsedLabel.textColor = RGBA(153, 153, 153, 1);
    _alreadyUsedLabel.hidden = YES;
    [self addSubview:_alreadyUsedLabel];
    _alreadyUsedLabel.top = _systemLabel.bottom + 5;
    _alreadyUsedLabel.left = _hourSlider.left;
    
    _clearBtn = [ZYViewUtils setupConfirmBtn];
    _clearBtn.size = CGSizeMake(_hourSlider.width * 0.27, _hourSlider.height * 1.25);
    [self addSubview:_clearBtn];
    _clearBtn.centerY = _alreadyUsedLabel.centerY;
    _clearBtn.right = _hourSlider.right;
    [_clearBtn setTitle:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_QUICK_SETUP_CLEAR_BTN_TITLE] forState:UIControlStateNormal];
    [_clearBtn addTarget:self action:@selector(doClearClick:) forControlEvents:UIControlEventTouchUpInside];
    _clearBtn.hidden = YES;
    
    self.systemValue  = 2400;
    self.height = _clearBtn.bottom + 15;
}

-(void)setMaxValue:(CGFloat)maxValue{
    _maxValue = maxValue;
    [self updateView];
}

-(void)setMinValue:(CGFloat)minValue{
    _minValue = minValue;
    [self updateView];
}

-(void)setSystemValue:(CGFloat)systemTemperature{
    if(systemTemperature == 0){
        return;
    }
    _systemValue = systemTemperature;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    NSString *system = [language getStringByKey:LC_QUICK_SETUP_DEFAULT_LABEL];
    _systemLabel.text = [NSString stringWithFormat:@"%@ %.0fh",system,_systemValue];
    [self updateView];
}

-(void)updateView{
    ZYWeakSelf(weakSelf)
    [UIView animateWithDuration:0.3 animations:^{
        _systemImageView.centerX = _hourSlider.left + ((_systemValue-_minValue)/(_maxValue - _minValue)) * _hourSlider.width;
        weakSelf.systemLabel.centerX = weakSelf.systemImageView.centerX;
    }];
}

-(void)setModel:(ZYModeTypeModel *)model{
    _model = model;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    _modeTypeImageView.image = [UIImage imageNamed:model.imageName];
    _modeNameLabel.text = [language getStringByKey:model.title];
}

-(void)setCurrentValue:(CGFloat)currentValue{
    _currentValue = currentValue;
    _hourLabel.text = [NSString stringWithFormat:@"%.0fh",currentValue];
    [_hourSlider setValue:currentValue animated:YES];
}

-(void)setAlreadyUsed:(CGFloat)alreadyUsed{
    _alreadyUsed = alreadyUsed;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    NSString *already = [language getStringByKey:LC_QUICK_SETUP_ALREADY_USED_LABEL];
    _alreadyUsedLabel.text = [NSString stringWithFormat:@"%@:%.0fh",already,alreadyUsed];
}

#pragma mark - event
-(void)valueChange:(UISlider *)slider{
    _hourLabel.text = [NSString stringWithFormat:@"%.0fh",slider.value];
    _currentValue = slider.value;
    if([_delegate respondsToSelector:@selector(filterSettingView:changeValue:type:)]){
        [_delegate filterSettingView:self changeValue:slider.value type:_model.modeType];
    }
}

-(void)doClearClick:(UIButton *)btn{
    if([_delegate respondsToSelector:@selector(filterSettingView:doClearClick:type:)]){
        [_delegate filterSettingView:self doClearClick:btn type:_model.modeType];
    }
}

@end
